using Godot;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq; 
[GlobalClass]

public partial class BTSelector : BTComposite//选择节点，完成其中一项即可
{
	
	public List<BTNode> children;
	public override void run()
	{
		List<Node> children = new List<Node>(this.GetChildren());
    
		_index_child_node = ChooseNode(children);
		
		Run_current_childnode();
		
	}

	public override void On_child_completed(bool result)
	{
		if(result)
		{
			EmitSignal(SignalName.completed,succeed());
			return;
		}
		else
		{
			EmitSignal(SignalName.completed,faile());
			return;
		}
	}
	public virtual int ChooseNode(List<Node> children)//选择最高效用值（优先度）的行为
	{
		int index = 0,i = 0;
		int size = children.Count;
		double[]values = new double[size];
		double value = 0;
		
		foreach (Node child in children)//遍历子节点
		{
			if (child is BTNode btNode) // 使用模式匹配检查类型并直接转换  
            {  
                values[i] = btNode.Utility(); // 调用方法  
				i++;
            } 
			if(i > size)
			{break;}
			
		}

		for(i = 0 ;i< children.Count;i++)
		{
			if(values[i] > value)
			{
				value = values[i];
				index = i;
			}
		}
		
		return index;
	}

	public override double Utility()
    {
        double value = 0.0;
		return value;
    }

	
}
